package com.tianbing.adlottery.controller.storeadmin;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.jubao.ejb.business.UserBusiness;
import com.jubao.ejb.dao.StoreDao;
import com.jubao.ejb.entity.Store;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditStoreProfileController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditStoreProfileController.class.getName());


	private StoreDao storeDao;

	private UserBusiness userBusiness;

	private SkinUtils skinUtils;

	// IPartyService partyService;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserSession auth = skinUtils.getUserAuthorization(request, null);
		ControllerFormBean fb = new ControllerFormBean();
		long storeSysId = auth.getManagedStoreSysId();
		Store store = storeDao.find(storeSysId);
		if (store == null) {
			store = new Store();
		}
		fb.setStore(store);
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		ControllerFormBean form = (ControllerFormBean) cmd;
		UserSession auth = (UserSession) skinUtils.getUserAuthorization(request, response, true);
		Store store = form.getStore();

		ModelAndView mv = new ModelAndView(this.getSuccessView()+"?stosid="+store.getSysId());

		userBusiness.saveManagedStore(auth.getUserSysId(), store);
		skinUtils.refreshUserAuthorization(request, response);
		return mv;
	}

	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();
		return model;
	}

	/**
	 * @param logger the logger to set
	 */
	public static void setLogger(Logger logger) {
		EditStoreProfileController.logger = logger;
	}

	/**
	 * @param storeDao the storeDao to set
	 */
	public void setStoreDao(StoreDao storeDao) {
		this.storeDao = storeDao;
	}

	/**
	 * @param userBusiness the userBusiness to set
	 */
	public void setUserBusiness(UserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	/**
	 * @param skinUtils the skinUtils to set
	 */
	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

}
